home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_11_01
/
1101074a
< prev
next >
Wrap
Text File
|
1992-11-01
|
3KB
|
124 lines
/*****************************************************
File Name: STR_NGET.C
Description: Library of functions for geting
substrings in a string
Global Function List: str_nleft
str_nmid
str_nright
str_rstr
Portability: Standard C
******************************************************/
#include <stdlib.h>
#include <string.h>
#include <str_nget.h>
/*****************************************************
Name: str_nleft
Expanded Name: Get Left N Characters
Parameters: Str - string to get left charaters
Num - number of characters to get
Return: Str
Description: Get Num leftmost charcters in Str.
Modifies Str.
*****************************************************/
char *str_nleft( char *Str, size_t Num )
{
if ( Num < strlen( Str ))
{
Str[Num] = '\0';
}
return ( Str );
} /* function str_nleft */
/*****************************************************
Name: str_nmid
Expanded Name: Get Middle N Characters
Parameters: Str - string to get substring in
Pos - index into Str of start of midstr
Num - count of charcters to get
Return: Str
Description: Get Num chars from middle of string.
*****************************************************/
char *str_nmid( char *Str, size_t Pos, size_t Num )
{
char *Mid;
size_t Len = strlen( Str );
if ( Pos >= Len )
{
/* Outside of string */
*Str = '\0';
return ( Str );
}
/* Adjust count if it extends outside of string */
if ( Pos + Num > Len )
{
Num = Len - Pos;
}
Mid = &Str[Pos];
memmove( (void *)Str, (void *)Mid, Num );
Str[Num] = '\0';
return ( Str );
} /* function str_nmid */
/*****************************************************
Name: str_nright
Expanded Name: Get Right N Characters
Parameters: Str - string to get right charaters
Num - number of characters to get
Return: Str
Description: Get Num righmost charcters in Str.
Modifies Str.
*****************************************************/
char *str_nright( char *Str, size_t Num )
{
size_t Len = strlen( Str );
return ( str_nmid( Str,
( Num > Len ? 0 : Len - Num ),
min( Num, Len ) ) );
} /* function str_nright */
/*****************************************************
Name: str_rstr
Expanded Name: String Right (Reverse) Search
Parameters: Str - string to search
Find - string to search for
Return: Pointer to last occurance of substring
Find in Str or NULL if not found
Description: Searches for last occurrence of sub
string Find within Str.
*****************************************************/
char *str_rstr( char *Str, char *Find )
{
char *StrResult = NULL, *StrWork = Str;
while ( ( StrWork =
strstr( StrWork, Find ) ) != NULL )
{
StrResult = StrWork;
StrWork++;
}
return ( StrResult );
} /* function str_rstr */
/* End of File */